package gatperdut.chato.core.item.inventory;

import gatperdut.chato.constants.CSConstantsString;
import gatperdut.chato.core.ChatoServer;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.model.Chato.Item.BoxInventory;
import gatperdut.chato.model.Chato.Item.Item;

public class BoxInventoryController extends InventoryController {

    public BoxInventoryController( ChatoServer chatoServer, BoxInventory inventory ) {
        super( chatoServer, inventory );

        loadItems();
    }

    public void loadItems() {
        ItemController ic;
        for ( Item i : getInventory().getItems() ) {
            ic = new ItemController( getChatoServer(), i );
            putInInventory( ic );
            getChatoServer().getItemHandler().getItemCDOIDToController().put( i.cdoID().toURIFragment(), ic );
        }
    }

    @Override
    public BoxInventory getInventory() {
        return (BoxInventory) super.getInventory();
    }

    public void putInInventory( ItemController ic ) {
        ic.setContainerController( this );
        getItemControllers().add( ic );
        getInventory().getItems().add( ic.getItem() );
    }

    @Override
    public void takeFromInventory( ItemController ic ) {
        super.takeFromInventory( ic );
        getInventory().getItems().remove( ic.getItem() );
    }

    @Override
    public String listContentsShortDescription( boolean showDrinkableContents ) {
        StringBuilder contents = new StringBuilder( "" );
        boolean firstListed = false;
        for ( ItemController ic : getItemControllers() ) {
            if ( !firstListed ) {
                firstListed = true;
            }
            else {
                contents.append( CSConstantsString.NEWLINE );
            }
            contents.append( ic.getShortDesc( showDrinkableContents ) );
        }
        return contents.toString();
    }

}
